'use strict';
var path = require('path');
var webpack = require('webpack');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var version = require('./webpack.version');
var BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
var UglifyJsPlugin  = require('uglifyjs-webpack-plugin');
var FileListPlugin  = require('./FileListPlugin');
module.exports = {
    // devtool: 'cheap-module-eval-source-map',
    // devtool: 'none',
    mode: 'development',
    resolve: {
        alias:{ // 路径别名
            '@': path.resolve(__dirname + '/src'),
            '#': path.resolve(__dirname + '/src/templates'),
        },
    },
    context: path.resolve(__dirname + '/src'),
    entry: {
        'app': [
            './angular.js',
            './directives',
            './services',
            './filter',
            './app.js',
        ],
        'login': './login.js'
    },
    output: {
        path: path.resolve(__dirname + '/dist'),
        filename: 'js/[name].bundle.js',
        publicPath: '',
    },
    devServer: {
        historyApiFallback: true,
        inline: true, // 行
        open: true,
        progress: true,
        // host: '',
        port: 40131, // 我的项目专用端口
        proxy: {
            '/ng1x': 'http:// localhost:8086'
        }
    },
    module: {
        rules: [
            {
                test: /\.view.html$/,
                use: [
                    { loader: 'ngtemplate-loader' },
                    { loader: 'html-loader', options: { attrs: ['img:src', 'img:data-src', 'img:ng-src', 'audio:src'] } } // html图片输出
                ],
            }, {
                test: /\.css$/,
                use: [
                    { loader: 'style-loader' },
                    { loader: 'css-loader' }
                ]
            }, {
                test: /\.(scss)$/,
                use: [
                    { loader: 'style-loader', },
                    { loader: 'css-loader', },
                    { loader: 'postcss-loader', options: { plugins: function () { return [ require('precss'), require('autoprefixer') ]; } } },
                    { loader: 'sass-loader' }
                ]
            }, {
                test: /\.(png|jpe?g|gif|ico|cur)(\?.*)?$/,
                use: [
                    { loader: 'url-loader', options: { limit: 10, name: './images/[name].[ext]' } }
                ]
            }, {
                test: /\.(mp3|ogg)(\?.*$|$)/,
                use: [
                    { loader: 'file-loader', options: { name: './audio/[name].[ext]' } }
                ]
            }, {
                test: /\.(woff|woff2|eot|ttf|svg)(\?.*$|$)/,
                use: [
                    { loader: 'file-loader', options: { name: './fonts/[name].[ext]' } }
                ]
            }
        ]
    },
    optimization: {
        runtimeChunk: {
            name: "manifest"
        },
        // 打包 公共文件
        splitChunks: {
            /*chunks: "async",
            minSize: 100*1024, // 模块的最小体积
            maxSize: 3*1024*1024, // 模块的最大体积
            minChunks: 1, // 模块的最小被引用次数
            maxAsyncRequests: 5, // 按需加载的最大并行请求数
            maxInitialRequests: 3, // 一个入口最大并行请求数
            automaticNameDelimiter: '~', // 文件名的连接符
            name: true,*/
            cacheGroups: {
                step1:{ // 打包jquery核心库
                    chunks: "all", minSize: 0*1024, minChunks: 1,
                    test: /[\\/]node_modules[\\/](_jquery@.*|_moment@.*|_bootstrap@.*|_lodash@.*|_layui-layer@.*)/,
                    name:'jquery',
                    priority: -10
                },
                step2:{ // 打包jquery插件库
                    chunks: "all", minSize: 0*1024, minChunks: 1,
                    test: /[\\/]node_modules[\\/](_select2@.*|_jquery[^@].*|_google-code-prettify.*|_mockjs.*|_gojs.*|_chartist.*)/,
                    name:'jquery.f3',
                    priority: -20
                },
                step3:{ // 打包angular有关库
                    chunks: "all", minSize: 0*1024, minChunks: 1,
                    // 所有的文件 | 抽取最小体积限制 | 抽取最小复用限制 1就是直接提出来
                    test: /[\\/]node_modules[\\/](_angular.*|_@uirouter.*|_@bradchesney79.*)/,
                    name:'angular',
                    priority: -30
                },
                step4:{ // 打包jquery自定义插件库
                    chunks: "all", minSize: 0*1024, minChunks: 1,
                    test: /[\\/]src[\\/]vender[\\/]/,
                    name:'jquery.custom',
                    priority: -40
                },
                step5: { // 打包module模块
                    chunks: "all", minSize: 0*1024, minChunks: 1,
                    test: /[\\/]src[\\/](directives.*|services.*|filter.*)/,
                    name:'module',
                    priority: -50
                },
                step6: { // 打包webpackJson启动器
                    chunks: "all", minSize: 0*1024, minChunks: 1,
                    test: /[\\/](node_modules)[\\/]/,
                    name:'vender',
                    priority: -60
                },
                default: {
                    minChunks: 2,
                    priority: -5,
                    reuseExistingChunk: true
                }
            }
        }
    },
    plugins: [
        new HtmlWebpackPlugin({
            template: __dirname + '/src/index.tmpl.html',
            minify: { // 压缩HTML文件
                removeComments: true, // 移除HTML中的注释
                collapseWhitespace: true, // 删除空白符与换行符
                minifyCSS: true// 压缩内联css
            },
            chunks: ['manifest', 'jquery', 'login', 'vender']
        }),
        new webpack.ProvidePlugin({
            $: 'jquery',
            jQuery: 'jquery',
            mock: 'mock'
        }),
        // new BundleAnalyzerPlugin(),
        // new UglifyJsPlugin(),
        // new FileListPlugin()
    ]
};